package com.yjyw.tiexie.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.yjyw.tiexie.R;
import com.yjyw.tiexie.bean.event.IdentificationEvent;
import com.yjyw.tiexie.bean.event.LendMoneyEvent;
import com.yjyw.tiexie.bean.event.OrderFragmentEvent;
import com.yjyw.tiexie.common.BaseActivity;
import com.yjyw.tiexie.common.net.NetCallBack;
import com.yjyw.tiexie.util.StringUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created on 2019/9/17.
 *
 * @author ${AUTHOR}
 * TODO:
 */
public class H5Activity extends BaseActivity implements NetCallBack {
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.web_view)
    WebView webView;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private int type;

    @Override
    protected int getContentView() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra("type", 0);
        String title = getIntent().getStringExtra("title");
        if (StringUtil.isEmpty(title)) {
            tvTitle.setText(getIntent().getStringExtra(""));
        } else {
            tvTitle.setText(getIntent().getStringExtra("title"));
        }
        webView.setBackgroundColor(0);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.setScrollContainer(false);
        webView.getSettings().setJavaScriptEnabled(true);// 设置webview_xieyi控件可以加载JavaScript代码
        webView.getSettings().setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        webView.getSettings().setDisplayZoomControls(false);


        if (type == 0) {
            //获取协议内容
//            getContract();
            //加载网页 url
//            String url = getIntent().getStringExtra("url");
//            webView.loadUrl(url);
            webView.loadUrl("file:///android_asset/agreementAndPrivacy.html");
        } else {
            //加载网页 url
            String url = getIntent().getStringExtra("url");
            webView.loadUrl(url);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        if (webView != null) {
            // 如果先调用destroy()方法，则会命中if (isDestroyed()) return;这一行代码，需要先onDetachedFromWindow()，再
            // destory()
            ViewParent parent = webView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(webView);
            }
            webView.stopLoading();
            // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
            webView.getSettings().setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.clearView();
            webView.removeAllViews();
            webView.destroy();
        }
        super.onDestroy();
    }

    @Override
    public void onResponse(boolean flag, int requestCode, String result) throws Exception {
    }

    @Override
    public void tokenTimeOut(int requestCode) {

    }

    @OnClick({R.id.iv_left, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                finish();
                break;
            case R.id.tv_confirm:
                finish();
                EventBus.getDefault().post(new IdentificationEvent("agree"));
                EventBus.getDefault().post(new LendMoneyEvent("agree"));
                EventBus.getDefault().post(new OrderFragmentEvent("agree"));
                break;
        }
    }
}

